﻿#pragma once

#include "TrImpl.h"
#include "ThostFtdcTraderApi.h"


class CTrTraderSpi : public CThostFtdcTraderSpi
{
public:
    CTrTraderSpi();
    ~CTrTraderSpi();
    CThostFtdcReqUserLoginField     m_FtdcReqUserLoginField;
    CThostFtdcTraderApi* m_pFtdcTraderApi = nullptr;
    CTrImpl* m_pTrImpl = nullptr;
    int                             m_nResultID;
    int								m_nOrderRef;
    time_t                             m_nLastUpdatePositionTime;
    time_t                             m_nLastUpdateTradingAccount;
    time_t                             m_nLastInsertOrderTime;
    time_t                             m_nLastUserLoginSubmiTime;
    TThostFtdcTimeType              m_LoginTime;
    TThostFtdcBrokerIDType          m_BrokerID;
    TThostFtdcUserIDType            m_UserID;
    TThostFtdcAuthCodeType          m_AuthCode;
    TThostFtdcAppIDType             m_AppID;
    TThostFtdcFrontIDType           m_FrontID;
    TThostFtdcSessionIDType         m_SessionID;
    TThostFtdcOrderRefType          m_MaxOrderRef;
    CThostFtdcTradingAccountField   m_TradingAccount;
    std::vector<CThostFtdcInvestorPositionField>    m_InvestorPosition;
    std::vector<CThostFtdcOrderField>   m_FtdcOrder;
    std::vector<CThostFtdcTradeField>   m_FtdcTrade;
    Tr_UserLoginStatus				m_LoginStatus;
    Tr_InvestorPositionStatus		m_InvestorPositionStatus;
    bool                            m_bIsQuotting;
public:
    bool                            Buy(TThostFtdcInstrumentIDType InstrumentID, int nLots, double dPrice, int nOffset = 0, const char* source = nullptr);
    bool                            Sell(TThostFtdcInstrumentIDType InstrumentID, int nLots, double dPrice, int nOffset = 0, const char* source = nullptr);
    bool                            Short(TThostFtdcInstrumentIDType InstrumentID, int nLots, double dPrice, int nOffset = 0, const char* source = nullptr);
    bool                            Cover(TThostFtdcInstrumentIDType InstrumentID, int nLots, double dPrice, int nOffset = 0, const char* source = nullptr);
    void                            Revoke(CThostFtdcOrderField order);
    void                            SetUserLoginField(char* szBrokerID, char* szUserID, char* szPassword);
    bool                            IsErrorRspInfo(CThostFtdcRspInfoField* pRspInfo);
    bool                            bIsReadyTrade();
    void                            reqSettlementInfoConfirm();
    void                            reqRspQryInvestorPosition();
    void                            reqRspQryInstrument();
    void                            reqQueryTradingAccount();
    void                            reqOrderInsert(TThostFtdcInstrumentIDType InstrumentID, TThostFtdcOffsetFlagType CombOffsetFlag, TThostFtdcDirectionType Direction, TThostFtdcPriceType	LimitPrice, TThostFtdcVolumeType VolumeTotalOriginal, const char* Source);
    time_t                          timeStamp();
public:
    virtual void OnFrontConnected() override;
    virtual void OnRspAuthenticate(CThostFtdcRspAuthenticateField* pRspAuthenticateField, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnFrontDisconnected(int nReason) override;
    virtual void OnRspUserLogin(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspUserLogout(CThostFtdcUserLogoutField* pUserLogout, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField* pSettlementInfoConfirm, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspQrySettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField* pSettlementInfoConfirm, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspQryTradingAccount(CThostFtdcTradingAccountField* pTradingAccount, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField* pInvestorPosition, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspQryInstrument(CThostFtdcInstrumentField* pInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspError(CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRtnOrder(CThostFtdcOrderField* pOrder) override;
    virtual void OnRtnTrade(CThostFtdcTradeField* pTrade) override;
    virtual void OnRspQryOrder(CThostFtdcOrderField* pOrder, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspOrderInsert(CThostFtdcInputOrderField* pInputOrder, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnErrRtnOrderInsert(CThostFtdcInputOrderField* pInputOrder, CThostFtdcRspInfoField* pRspInfo) override;
    virtual void OnErrRtnOrderAction(CThostFtdcOrderActionField* pOrderAction, CThostFtdcRspInfoField* pRspInfo) override;
    virtual void OnRspOrderAction(CThostFtdcInputOrderActionField* pInputOrderAction, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
};
